<template>
	<view class="examlist">
		<!-- 我的考试 -->
		<view class="title">历史考试</view>
		<view class="list" v-if="arr.length !=0">
			<view class="box" v-for="item in arr" :key="item.id">
				<navigator :url="`/pages/result/result?id=${item._id}`">
					<view class="p">试卷: {{item.name}}</view>
					<view class="p p-2">用户: {{item.time}}</view>
					<view class="p">分数: <text class="t">{{item.score}}</text></view>
					<view class="p p-2">日期: {{item.datetime}}</view>
				</navigator>
			</view>
		</view>
		<view v-else>
			<view class="message">暂无成绩列表~~</view>
		</view>
	</view>
</template>

<script>
	// 创建db对象
	const db = uniCloud.database();
	// 获取 `tt_scores` 集合的引用
	const scoresCollection = db.collection('tt_scores');

	export default {
		data() {
			return {
				arr: []
			}
		},
		methods: {
			// 获取考试成绩列表
			async getExamListFn(account) {
				uni.showLoading({
					title: "正在加载.."
				})
				let that = this;
				const resData = await scoresCollection.where({
					account: account
				}).get()
				if (resData.result && resData.result.data) {
					let data = resData.result.data;
					console.log(data)
					let arr2 = data.map(item => {
						const dt = new Date()
						dt.setTime(item.createAt) // 时间戳
						const d = dt.toLocaleDateString();
						const t = dt.toLocaleTimeString()
						item.datetime = d + " " + t;
						return item;
					})
					that.arr = arr2;
				}
				uni.hideLoading();

			},
			// 前端调用分页云函数
			async loadPageData(page = 1, pageSize = 10) {
				let that = this;
				try {
					let info = uni.getStorageSync("INFO")
					const account = info.account
					const res = await uniCloud.callFunction({
						name: 'score',
						data: {
							collectionName: 'tt_scores', // 你的集合名
							page,
							pageSize,
							filter: {
								account: account // 筛选账号对应的数据
							},
							// orderBy: 'createAt',
							// orderType: 'desc'
						}
					})
					if (res.result.code === 0) {
						let data = res.result.data;
						console.log(data)
						let arr2 = data.list.map(item => {
							const dt = new Date()
							dt.setTime(item.createAt)
							const d = dt.toLocaleDateString();
							const t = dt.toLocaleTimeString()
							item.datetime = d + " " + t;
							return item;
						})
						that.arr = arr2;
					} else {
						console.error('获取分页数据失败:', res.result.message)
						return null
					}
				} catch (e) {
					console.error('调用云函数失败:', e)
					return null
				}
			}

		},
		onLoad(options) {
			let info = uni.getStorageSync("INFO")
			if (info) {
				this.loadPageData();
			} else {
				uni.showToast({
					title: "您还没登录",
					icon: "none"
				})
			}
		}
	}
</script>


<style lang="scss">
	@import "../../static/scss/variable.scss";

	.examlist {
		padding: $padd;
	}

	.list {
		.box {
			margin-top: 20rpx;
			padding: 20rpx;
			border: 1px solid #f0f0f0;
			box-shadow: 0px 2px 2px #ccc;

			.p {
				padding-bottom: 10rpx;
			}

			.p-2 {
				font-size: 28rpx;
				color: #666;
			}

			.t {
				font-size: 50rpx;
			}
		}
	}
</style>